import json
import sys

import facade
import parsel
import pymysql
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
configfile = BaseDir.get_new_path(curPath, "db.ini")
topPath = BaseDir.get_upper_dir(curPath, -2)
sPath = BaseDir.get_new_path(topPath, "download", "adks_cqjd", "download", "author")
BaseDir.create_dir(sPath)


class DownAuthorMsg(object):
    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutiles = facade.MysqlUtiles(configfile, "db", logger=self.logger)
        self.proxys = {
            "http": "192.168.30.176:8171",
            "https": "192.168.30.176:8171"
        }

    def select(self):
        sql = "SELECT DISTINCT authorurl from article WHERE stat=0"
        rows = self.mysqlutiles.SelectFromDB(sql)
        for row in rows:
            url = row[0]
            fullurl = "http://202.202.244.23" + url
            BoolResult, errString, r = facade.BaseRequest(fullurl, timeout=30, proxies=self.proxys)
            if BoolResult:
                selector = parsel.Selector(text=r.text)
                listauthormsg = selector.xpath('//*[@id="wrapper"]/div/div[1]/div[1]/p/text()').getall()
                authormsg = "".join(listauthormsg)
                print(authormsg)
                sql = "update article set authormsg='{}',stat=1 where authorurl='{}'".format(pymysql.escape_string(authormsg),url)
                self.mysqlutiles.ExeSqlToDB(sql)
            else:
                print("下载失败，请检查")


if __name__ == "__main__":
    down = DownAuthorMsg()
    down.select()